package com.xc.freeapp.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xc.freeapp.common.PageInfo;
import com.xc.freeapp.dao.MenuInfoDao;
import com.xc.freeapp.dao.dataSource.DBContextHolder;
import com.xc.freeapp.entity.MenuInfo;
import com.xc.freeapp.service.MenuService;

/**
 * 数据处理具体实现
 * 
 * @author ZhouXinjie
 *
 */

@Service
public class MenuServiceImpl implements MenuService {

	Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private MenuInfoDao menuInfoDao;

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<MenuInfo> getUserMenus(String userId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("获取当前用户拥有的菜单");
		return menuInfoDao.getUserMenus(userId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<MenuInfo> queryAllMenus(PageInfo<MenuInfo> pageInfo, MenuInfo menu) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("菜单查询");
		if (null != pageInfo) {
			pageInfo.setTotalRecords(menuInfoDao.getTotalRecords(menu));
		}
		RowBounds rb = new RowBounds(pageInfo.getFromRecord(), pageInfo.getPageSize());
		return menuInfoDao.queryAllMenus(rb, menu);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public List<MenuInfo> querySystemMenus(PageInfo<MenuInfo> pageInfo, MenuInfo menu) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		
		log.info("菜单查询");
		if (null != pageInfo) {
			pageInfo.setTotalRecords(menuInfoDao.getSystemRecords(menu));
		}
		RowBounds rb = new RowBounds(pageInfo.getFromRecord(), pageInfo.getPageSize());
		return menuInfoDao.querySystemMenus(rb, menu);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public MenuInfo getMenu(String menuId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("获取菜单信息");
		return menuInfoDao.getMenu(menuId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<MenuInfo> getAllMenus() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return menuInfoDao.getMenusByMenuType(null);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<MenuInfo> getAllSystemMenus() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return menuInfoDao.getMenusByMenuType("system");
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<MenuInfo> getAllWebMenus() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return menuInfoDao.getMenusByMenuType("web");
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int saveMenu(MenuInfo menu) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("添加菜单信息");
		return menuInfoDao.saveMenu(menu);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int updateMenu(MenuInfo menu) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("更新菜单信息");
		return menuInfoDao.updateMenu(menu);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int deleteMenu(String menuId) {
		DBContextHolder.switchDataSourceRW();//切换数据源
		log.info("删除菜单信息");
		return menuInfoDao.deleteMenu(menuId);
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
	public int getMaxMenuOrder() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return menuInfoDao.getMaxMenuOrder();
	}

	@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
	public List<String> getMenuTypes() {
		DBContextHolder.switchDataSourceRW();//切换数据源
		return menuInfoDao.getMenuTypes();
	}

}
